#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/wait.h>
#include <linux/semaphore.h>
#include <linux/sched.h>
#include <linux/cdev.h>
#include <linux/types.h>
#include <linux/kdev_t.h>
#include <linux/device.h>
#include <linux/uaccess.h>

#define MAJOR_NUM 466

struct dev
{
    struct cdev devm;9
    struct semaphore sem;
    wait_queue_head_t outq;
    int flag;
    char buffer[MAXNUM + 1];
    char *rd, *wr, *end;
} globalvar;

static struct class *my_class;
int major = MAJOR_NUM;

static ssize_t GlobalVar_ReadFunc(struct file *, char*, size_t, loff_t *);
static ssize_t GlobalVar_WriteFunc(struct file *, const char*, size_t, loff_t *);
static int GlobalVar_OpenFunc(struct inode* inode, struct file *filp);
static int GlobalVar_ReleaseFunc(struct inode *inode, struct file *filp);

struct file_operations globalvar_fops = 
{
    .read = GlobalVar_ReadFunc,
    .write = GlobalVar_WriteFunc,
    .open = GlobalVar_OpenFunc,
    .release = GlobalVar_ReleaseFunc,
};

static ssize_t GlobalVar_ReadFunc(struct file * f, char*, size_t size, loff_t * off)
{
    if (wait_event_interruptible(globalvar.outq, globalvar.flag) != 0)
    {
        return -ERESTARTSYS;
    }
    if (down_interruptible(&globalvar.sem))
    {
        return -ERESTARTSYS;
    }
    
    globalvar.flag = 0;
    printk("Into the read function\n");
    printk("the rd is %c\n", *globalvar.rd);
    if (globalvar.rd < globalvar.wr)
    {
        /* code */
    }
    
}
